package com.longrise.android.byjk.plugins.dealsituation.course.video2.education.insurance.videocontroller;

import android.content.Context;
import android.media.AudioManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.longrise.android.byjk.plugins.dealsituation.course.video2.media.IjkVideoView;


/**
 * Created by godliness on 2017/9/3.
 * 控制：手势音量
 * 亮度
 * 进度
 */

public class DetailController extends VideoController {

    /**
     * 控制亮度与音量
     */
    private View mDetailView;
    private ImageView mIv;
    private SeekBar mProgress;

    private boolean mVisiable;

    private float lastX;
    private float lastY;

    private boolean isFirst = true;// 是否第一次设置过总时间和总音量
    private boolean isAdjust = false;// 是否符合滑动范围

    private int threshold;

    private int mScreenWidth;
    private int mScreenHeight;// 屏幕的宽高

    private AudioManager mAudioManager;
    private int mMaxVolume;
    private int mCurVolume;

    @Override
    public void addVideoController(IjkVideoView videoView) {
        super.addVideoController(videoView);

        ViewGroup rootView = (ViewGroup) getControllerRootView();
        initVolumeAndBrighView(LayoutInflater.from(getContext()));
        thisAttachToRootView(rootView);
    }

    private void initVolumeAndBrighView(LayoutInflater inflater) {
        if (mDetailView == null) {
//            mDetailView = inflater.inflate(0, null, false);
//            mIv = mDetailView.findViewById(R.id.iv_vol_other);
//            mProgress = mDetailView.findViewById(R.id.sk_vol_other);
        }
    }

    private void initVolume(){
        mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);

        mMaxVolume  = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        mCurVolume  = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    }

    private void initScreen(){
        mScreenWidth = getContext().getResources().getDisplayMetrics().widthPixels;
        mScreenHeight = getContext().getResources().getDisplayMetrics().heightPixels;
    }

    private void thisAttachToRootView(ViewGroup rootView) {
        if (mDetailView != null) {
            rootView.addView(mDetailView);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;
                break;

            case MotionEvent.ACTION_MOVE:
                float changeX = x - lastX;
                float changeY = y - lastY;
                float absX = Math.abs(changeX);
                float absY = Math.abs(changeY);

                if (absX > threshold && absY > threshold) {
                    if (absY > absX) {// 说明是上下滑动
                        isAdjust = true;
                    } else {
                        isAdjust = false;
                    }
                } else if (absX < threshold && absY > threshold) {
                    isAdjust = true;
                } else if (absX > threshold && absY < threshold) {
                    isAdjust = false;
                }

                if (isAdjust) {
                    if (x < mScreenWidth / 2) {// 左屏幕，调节亮度
                        changeBrightness(-changeY);
                    } else {// 右屏幕，调节音量
                        changeVolume(-changeY);
                    }
                }

                lastX = x;
                lastY = y;

                break;

            case MotionEvent.ACTION_UP:
                lastX = 0;
                lastY = 0;
                gone();
                break;
        }
        return super.onTouch(v, event);
    }

    private void changeBrightness(float newProgress) {
        WindowManager.LayoutParams attributes = getWindow().getAttributes();
        float brightness = attributes.screenBrightness;
        float index = newProgress / mScreenHeight;
        brightness = Math.max(brightness + index, WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF);
        brightness = Math.min(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL, brightness);
        attributes.screenBrightness = brightness;
        getWindow().setAttributes(attributes);

        visiable(0);
        setProgress((int) (brightness * 100));
    }

    private void changeVolume(float progress) {

    }

    private void visiable(int resource) {
        if (!mVisiable && isAnor()) {
            mDetailView.setVisibility(View.GONE);
            AlphaAnimation alphaVisiable = getAlphaToVisiable();
            mDetailView.setAnimation(alphaVisiable);
            alphaVisiable.start();
            mVisiable = true;
        }
        setOptionsImageView(resource);
    }

    private void gone() {
        if (mVisiable && isAnor()) {
            mDetailView.setVisibility(View.GONE);
            AlphaAnimation alphaGone = getAlphaToGone();
            mDetailView.setAnimation(alphaGone);
            alphaGone.start();
            mVisiable = false;
        }
    }

    private AlphaAnimation getAlphaToGone() {
        AlphaAnimation alphaGone = new AlphaAnimation(1, 0);
        alphaGone.setDuration(100);
        return alphaGone;
    }

    private AlphaAnimation getAlphaToVisiable() {
        AlphaAnimation alphaVisiable = new AlphaAnimation(0, 1);
        alphaVisiable.setDuration(100);
        return alphaVisiable;
    }

    private boolean isAnor() {
        return mDetailView != null;
    }

    private void setOptionsImageView(int resource) {
        if (mIv != null) {
            mIv.setImageResource(resource);
        }
    }

    private void setProgress(int progress) {
        if (mProgress != null) {
            mProgress.setProgress(progress);
        }
    }
}
